package com.aishua.andpaydemo.utils;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogUtils {

    private static final String TAG = "IOT";

    private static boolean LOGCAT_ON = true;

    private static boolean LOGFILE_ON = true;

    public static void v(String msg) {
        if (LOGCAT_ON) {
            Log.v(TAG, msg);
        }
        if (LOGFILE_ON) {
        	 saveFile(msg);
        }
    }

    public static void i(String msg) {
        if (LOGCAT_ON) {
            Log.i(TAG, msg);
        }
        if (LOGFILE_ON) {
        	 saveFile(msg);
        }
    }

    public static void w(String msg) {
        if (LOGCAT_ON) {
            Log.w(TAG, msg);
        }
        if (LOGFILE_ON) {
        	 saveFile(msg);
        }
    }

    public static void e(String msg) {
        if (LOGCAT_ON) {
            Log.e(TAG, msg);
        }
        if (LOGFILE_ON) {
        	 saveFile(msg);
        }
    }

    public static void d(String msg) {
        if (LOGCAT_ON) {
            Log.d(TAG, msg);
        }
        if (LOGFILE_ON) {
        	 saveFile(msg);
        }
    }


    public static void saveFile(String str) {
        String filePath = null;
        boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (hasSDCard) {
            filePath = Environment.getExternalStorageDirectory().toString() + File.separator + TAG+"/log/"
                    + getTime("yyyy-MM-dd") + ".txt";
        } else
            filePath = Environment.getDownloadCacheDirectory().toString() + File.separator + TAG+"/log/"
                    + getTime("yyyy-MM-dd") + ".txt";

        try {
            File file = new File(filePath);
            if (!file.exists()) {
                File dir = new File(file.getParent());
                dir.mkdirs();
                file.createNewFile();
            }
            RandomAccessFile randomFile = null;  
            try {     
                // 打开一个随机访问文件流，按读写方式     
                randomFile = new RandomAccessFile(file, "rwd");     
                // 文件长度，字节数     
                long fileLength = randomFile.length();     
                // 将写文件指针移到文件尾。     
                randomFile.seek(fileLength);   
                str= getTime("yyyy-MM-dd-hh:mm:ss")+"=>"+str+"\n";
                randomFile.write(str.getBytes());      
            } catch (IOException e) {     
                e.printStackTrace();     
            } finally{  
                if(randomFile != null){  
                    try {  
                        randomFile.close();  
                    } catch (IOException e) {  
                        e.printStackTrace();  
                    }  
                }  
            }  
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String getTime(String xformat) {
        SimpleDateFormat format = new SimpleDateFormat(xformat);
        String time = format.format(new Date());
        return time;
    }
}
